package com.sgy.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sgy.common.ApiResult;
import com.sgy.service.UserService;
import com.sgy.util.StreamUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserAction {

    private Logger log = LoggerFactory.getLogger(UserAction.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ApiResult getUserInfo(HttpServletRequest request){
        StringBuffer req = StreamUtil.getRequestBody(request);
        log.info("请求报文为:" + req.toString());
        JSONObject reqData = JSON.parseObject(req.toString());
        String username = reqData.getString("username");
        String password = reqData.getString("password");
        if(StringUtils.isEmpty(username)){
            return ApiResult.fail("用户名不能为空");
        }
        if(StringUtils.isEmpty(password)){
            return ApiResult.fail("密码不能为空");
        }
        userService.login(username,password);
        return ApiResult.success();
    }
}
